<?php

namespace Admin\Controller;

class HomeController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Home");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取列表
        $list = $this->currentModel->queryForList($page, $requestMap);

//        $this->assign("statusEnum", C("DEFAULT_YN"));

//        $homeList = D("Home")->alias("h")
//            ->join("left join __CASE__ c on c.case_id = h.link")
//            ->field("h.*")
//            ->select();
        $this->assign("homeType", C("HOME_TYPE"));
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    public function add() {
        $this->assign("homeTypeEnum", C("HOME_TYPE"));
        $this->display();
    }

    public function remove() {

        $id = $_REQUEST['id'];

        D("Home")->deleteById($id);

        $this->success('删除成功!', U("home/index"));

    }

     /**
     * 添加数据
     */
    public function addPost() {

        $data["class_id"] = $_REQUEST["classId"];
        $data["code"] = $_REQUEST["code"];
        $data["name"] = $_REQUEST["productName"];
        $data["number"] = $_REQUEST["number"];
        $data["function"] = $_REQUEST["function"];
        $data["indication"] = $_REQUEST["indication"];
        $data["size"] = $_REQUEST["size"];
        $data["unit"] = $_REQUEST["unit"];
        $data["price"] = $_REQUEST["price"];
        $data["supplier"] = $_REQUEST["supplier"];
        $data["image_url"] = $_REQUEST["imageUrl"];

        $this->currentModel->add($data);

        $this->success("添加成功", U("home/index"));

    }

    /**
     * 更新数据
     */
    public function editPost() {

        $id = $_REQUEST["id"];

        $data = $this->currentModel->queryById($id);
        $data["class_id"] = $_REQUEST["classId"];
        $data["code"] = $_REQUEST["code"];
        $data["name"] = $_REQUEST["productName"];
        $data["number"] = $_REQUEST["number"];
        $data["function"] = $_REQUEST["function"];
        $data["indication"] = $_REQUEST["indication"];
        $data["size"] = $_REQUEST["size"];
        $data["unit"] = $_REQUEST["unit"];
        $data["price"] = $_REQUEST["price"];
        $data["supplier"] = $_REQUEST["supplier"];
        $data["image_url"] = $_REQUEST["imageUrl"];

        $this->currentModel->updateInfo($data);

        $this->success("更新成功", U("home/index"));

    }
}